package cc.rengu.igas.channel.upqc.core.service.trans;

import cc.rengu.igas.channel.upqc.core.model.ChnlBizResponse;
import cc.rengu.igas.channel.upqc.core.service.base.NoticeService;
import cc.rengu.igas.channel.upqc.facade.request.C2BTransNoticeRequest;
import cc.rengu.igas.channel.upqc.facade.response.C2BTransNoticeResponse;
import cc.rengu.igas.share.facade.request.UpqcC2bTransNoticeRequest;
import cc.rengu.igas.share.facade.response.UpqcC2bTransNoticeResponse;
import cc.rengu.igas.share.facade.result.Result;
import cc.rengu.oltp.service.common.enums.ChannelEnum;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.oltp.utility.util.*;

/**
 * 银联二维码－银联发起付款通知服务，基于原交易类型进行业务判断
 * 消费交易：0310000903          预授权冲正交易：0290100903
 * 消费冲正交易：0320000903      预授权撤销交易：0370000903
 * 消费撤销交易：0330000903      预授权完成交易：0380000903
 * 退货交易：0340000903         预授权完成冲正交易：0380100903
 * 预授权交易：0290000903       预授权完成撤销交易：0390000903
 */
public class C2BPayNoticeService extends NoticeService {

    @Override
    protected Object beforeBizProcess() throws Exception {
        C2BTransNoticeRequest c2bTransNoticeRequest = new C2BTransNoticeRequest();
        ConvertUtil.convertOutput(c2bTransNoticeRequest);
        return c2bTransNoticeRequest;
    }

    @Override
    protected ChnlBizResponse callBizService(Object request) throws Exception {
        C2BTransNoticeRequest c2bTransNoticeRequest = (C2BTransNoticeRequest) request;
        C2BTransNoticeResponse c2BTransNoticeResponse = new C2BTransNoticeResponse();

        /* 调用Share模块中的银联二维码业务公共处理服务 */
        UpqcC2bTransNoticeRequest upqcC2bTransNoticeRequest = new UpqcC2bTransNoticeRequest();
        BeanUtil.beanCopy(c2bTransNoticeRequest, upqcC2bTransNoticeRequest);
        upqcC2bTransNoticeRequest.getHeader().setTxnNum("P01SH001");
        upqcC2bTransNoticeRequest.getHeader().setTraceNo(ChannelEnum.UPQC.getChannelType() + DateUtil.getCurrentDateTime("yyyyMMddHHmmssSSS") + RandomUtil.randomNumStr(10));
        DubboService dubboService = new DubboServiceImpl();
        Result<UpqcC2bTransNoticeResponse> callResult = (Result<UpqcC2bTransNoticeResponse>) dubboService.callDubboService("upqcUnifiedService", "cb2TransNotice", upqcC2bTransNoticeRequest);
        if (null != callResult) {
            UpqcC2bTransNoticeResponse upqcC2bTransNoticeResponse = callResult.getResult();
            BeanUtil.beanCopy(upqcC2bTransNoticeResponse, c2BTransNoticeResponse);
        }

        /* 组装应答报文 */
        ChnlBizResponse chnlBizResponse = new ChnlBizResponse();
        chnlBizResponse.setResult(c2BTransNoticeResponse);
        return chnlBizResponse;
    }

    @Override
    protected void afterBizProcess(ChnlBizResponse response) throws Exception {
        C2BTransNoticeResponse c2BTransNoticeResponse = (C2BTransNoticeResponse) response.getResult();
        ConvertUtil.convertInput(c2BTransNoticeResponse);
    }
}
